<?php namespace Phpcmf\Controllers\Member;
/* *
 *
 * 本Demo的语法参考： http://help.duixin.cc/445.html
 *
 * */
class Shenqing extends \Phpcmf\Table
{

    public function __construct()
    {
        parent::__construct();
        // 表单显示名称
        $this->name = dr_lang('申请');
        // 模板前缀(避免混淆)
        $this->tpl_prefix = 'shenqing_';
		// 初始化模块
		$this->_module_init(APP_DIR);
		// 支持附表存储
		//$this->is_data = 1;
		// 是否支持模块表
		//$this->is_module_index = 1;
		// 是否支持模型字段
		//$this->is_category_data_field = $this->module['category_data_field'] ? 1 : 0;

        // 采用ajax列表请求
        $this->is_ajax_list = true;

        // 用于表储存的字段，后台可修改的表字段，设置字段类别参考：http://help.duixin.cc/1138.html
        $field = array (
  'catid' => 
  array (
    'name' => '栏目id',
    'fieldname' => 'catid',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'title' => 
  array (
    'name' => '主题',
    'fieldname' => 'title',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'thumb' => 
  array (
    'name' => '缩略图',
    'fieldname' => 'thumb',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'keywords' => 
  array (
    'name' => '关键字',
    'fieldname' => 'keywords',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'description' => 
  array (
    'name' => '描述',
    'fieldname' => 'description',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'hits' => 
  array (
    'name' => '浏览数',
    'fieldname' => 'hits',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'uid' => 
  array (
    'name' => '作者id',
    'fieldname' => 'uid',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'author' => 
  array (
    'name' => '作者名称',
    'fieldname' => 'author',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'status' => 
  array (
    'name' => '状态',
    'fieldname' => 'status',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'url' => 
  array (
    'name' => '地址',
    'fieldname' => 'url',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'link_id' => 
  array (
    'name' => '同步id',
    'fieldname' => 'link_id',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'tableid' => 
  array (
    'name' => '附表id',
    'fieldname' => 'tableid',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'inputip' => 
  array (
    'name' => '客户端ip信息',
    'fieldname' => 'inputip',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'joins' => 
  array (
    'name' => '加入',
    'fieldname' => 'joins',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'fuwuzan' => 
  array (
    'name' => '圈赞',
    'fieldname' => 'fuwuzan',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'inputtime' => 
  array (
    'name' => '录入时间',
    'fieldname' => 'inputtime',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'updatetime' => 
  array (
    'name' => '更新时间',
    'fieldname' => 'updatetime',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'comments' => 
  array (
    'name' => '评论数量',
    'fieldname' => 'comments',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'favorites' => 
  array (
    'name' => '收藏数量',
    'fieldname' => 'favorites',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'avgsort' => 
  array (
    'name' => '平均点评分数',
    'fieldname' => 'avgsort',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'displayorder' => 
  array (
    'name' => '排序值',
    'fieldname' => 'displayorder',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'son_total' => 
  array (
    'name' => '表单章节统计',
    'fieldname' => 'son_total',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'son_type' => 
  array (
    'name' => '服务类别',
    'fieldname' => 'son_type',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'zt' => 
  array (
    'name' => '连载状态',
    'fieldname' => 'zt',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'price' => 
  array (
    'name' => '售价',
    'fieldname' => 'price',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
  'beijingyanse' => 
  array (
    'name' => '背景颜色',
    'fieldname' => 'beijingyanse',
    'ismain' => 1,
    'ismember' => 1,
    'fieldtype' => 'Text',
  ),
);

        // 用于列表显示的字段
        $list_field = array (
  'catid' => 
  array (
    'use' => '1',
    'name' => '栏目id',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'title' => 
  array (
    'use' => '1',
    'name' => '主题',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'thumb' => 
  array (
    'use' => '1',
    'name' => '缩略图',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'keywords' => 
  array (
    'use' => '1',
    'name' => '关键字',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'description' => 
  array (
    'use' => '1',
    'name' => '描述',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'hits' => 
  array (
    'use' => '1',
    'name' => '浏览数',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'uid' => 
  array (
    'use' => '1',
    'name' => '作者id',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'author' => 
  array (
    'use' => '1',
    'name' => '作者名称',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'status' => 
  array (
    'use' => '1',
    'name' => '状态',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'url' => 
  array (
    'use' => '1',
    'name' => '地址',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'link_id' => 
  array (
    'use' => '1',
    'name' => '同步id',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'tableid' => 
  array (
    'use' => '1',
    'name' => '附表id',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'inputip' => 
  array (
    'use' => '1',
    'name' => '客户端ip信息',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'joins' => 
  array (
    'use' => '1',
    'name' => '加入',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'fuwuzan' => 
  array (
    'use' => '1',
    'name' => '圈赞',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'inputtime' => 
  array (
    'use' => '1',
    'name' => '录入时间',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'updatetime' => 
  array (
    'use' => '1',
    'name' => '更新时间',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'comments' => 
  array (
    'use' => '1',
    'name' => '评论数量',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'favorites' => 
  array (
    'use' => '1',
    'name' => '收藏数量',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'avgsort' => 
  array (
    'use' => '1',
    'name' => '平均点评分数',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'displayorder' => 
  array (
    'use' => '1',
    'name' => '排序值',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'son_total' => 
  array (
    'use' => '1',
    'name' => '表单章节统计',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'son_type' => 
  array (
    'use' => '1',
    'name' => '服务类别',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'zt' => 
  array (
    'use' => '1',
    'name' => '连载状态',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'price' => 
  array (
    'use' => '1',
    'name' => '售价',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
  'beijingyanse' => 
  array (
    'use' => '1',
    'name' => '背景颜色',
    'width' => '',
    'func' => '',
    'center' => '0',
  ),
);
        /*
         *array (
                    'use' => '1', // 1是显示，0是不显示
                    'name' => '', //显示名称
                    'width' => '', // 显示宽度
                    'func' => '', // 回调函数见：http://help.duixin.cc/463.html
                    'center' => '0', // 1是居中，0是默认
                )
         * */


        $json_file = str_replace('.php', '.json', __FILE__);
        if (is_file($json_file)) {
            $json = dr_string2array(file_get_contents($json_file));
            if ($json) {
                if ($json['field']) {
                    $field = $json['field'];
                }
                if ($json['list']) {
                    $list_field = $json['list'];
                }
            }
        }

        // 初始化数据表
        $this->_init([
            'table' => '1_quan',  // （不带前缀的）表名字
            'field' => $field, // 可查询的字段
            'list_field' => $list_field,
            'order_by' => 'id desc', // 列表排序，默认的排序方式
            'date_field' => '', // 按时间段搜索字段，没有时间字段留空
            'where_list' => 'uid='.$this->uid, // 默认搜索条件，每次列表都执行
        ]);
        $this->edit_where = ''; //修改数据时候的条件判断
        $this->delete_where = ''; //删除数据时候的条件判断
        $this->list_where = ''; //数据列表时候的条件判断

        // 把公共变量传入模板
        \Phpcmf\Service::V()->assign([
            // 搜索字段
            'field' => $field,
            'is_time_where' => $this->init['date_field'],
        ]);
    }

    // 查看列表
    public function index() {
        list($tpl) = $this->_List();
        \Phpcmf\Service::V()->display($tpl);
    }

    // 添加内容
    public function add() {
		$this->tablename = SITE_ID.'_'.APP_DIR;
		//判断是否有审核中的圈子
		$shenhe = \Phpcmf\Service::M()->db->table($this->tablename)->where('zt', 0)->where('uid', $this->uid)->get()->getRowArray();
		if ($shenhe){
			$this->_msg(0, dr_lang('您的圈子已经在审核中，请内心等待'));
		}
		$tongguo = \Phpcmf\Service::M()->db->table($this->tablename)->where('zt', 1)->where('uid', $this->uid)->countAllResults();
		if ($tongguo > 3){
			$this->_msg(0, dr_lang('您的圈子已经超出3个'));
		}
        $id = 0;
		if (IS_AJAX_POST) {
			$datas = \Phpcmf\Service::L('Input')->post('data');
			
			if (!$datas['title']){
				$this->_msg(0, dr_lang('圈子名字不能为空'));
			}
			$catid = $datas['catid'];
			$description = $datas['description'];
			if (!$description){
				$this->_msg(0, dr_lang('圈子简介不能为空'));
			}
		}
        $did = intval(\Phpcmf\Service::L('input')->get('did'));
        //$catid = intval(\Phpcmf\Service::L('input')->get('catid'));
        	
        $did && $this->auto_save = 0; // 草稿数据时不加载
        $draft = $did ? $this->content_model->get_draft($did) : [];
        $catid = $draft['catid'] ? $draft['catid'] : $catid;
        $select = '';
        	
        if (!$this->is_hcategory) {
            // 走栏目权限
            $category = $this->_get_module_member_category($this->module, 'add');
            // 栏目id不存在时就去第一个可用栏目为catid
            if (!$catid) {
                list($select, $catid) = \Phpcmf\Service::L('Tree')->select_category(
                    $category,
                    $catid,
                    'id=\'dr_catid\' name=\'data[catid]\' onChange="show_category_field(this.value)"',
                    '', 1, 1, 1
                );
            } else {
                $select = \Phpcmf\Service::L('Tree')->select_category(
                    $category,
                    $catid,
                    'id=\'dr_catid\' name=\'data[catid]\' onChange="show_category_field(this.value)"',
                    '', 1, 1
                );
            }
        	if (!$catid) {
        	    if ($this->uid) {
                    $this->_msg(0, dr_lang('没有可用的栏目发布权限'));
                } else {
                    $this->_msg(0, dr_lang('您没有登录'), dr_member_url('login/index'));
                }
            } elseif (!$category[$catid]) {
                $this->_msg(0, dr_lang('当前栏目(%s)没有发布权限', $this->module['category'][$catid]['name']));
            }
            $this->is_post_code = \Phpcmf\Service::M('member_auth')->category_auth($this->module, $catid, 'code', $this->member);
        } else {
            // 不走栏目权限，走自定义权限
            $rt = $this->content_model->_hcategory_member_add_auth();
            if (!$rt['code']) {
                $this->_msg(0, $rt['msg'], $rt['data']);
            }
            $this->is_post_code = $this->content_model->_hcategory_member_post_code();
        }
        	
        $this->is_get_catid = $catid;
        $draft && $draft['catid'] = $catid;
        	
        list($tpl) = $this->_Post($id, $draft);
        	
        \Phpcmf\Service::V()->assign([
            'did' => $did,
            'mcid' => 'add',
            'form' =>  dr_form_hidden(['is_draft' => 0, 'module' => $this->module['dirname'], 'id' => $id]),
            'catid' => 0,
            'select' => $select,
            'draft_url' => \Phpcmf\Service::L('Router')->member_url($this->module['dirname'].'/home/adddiy'),
            'draft_list' => $this->content_model->get_draft_list('cid='.$id),
            'is_post_code' =>  $this->is_post_code,
            'category_field_url' =>  $this->is_post_code || $this->module['category_data_field'] ? dr_member_url($this->module['dirname'].'/home/adddiy') : ''
        ]);
        if (!$this->uid && is_file(dr_tpl_path().'guest_post.html')) {
            \Phpcmf\Service::V()->display('guest_post.html');
        } else {
            \Phpcmf\Service::V()->display('shenqing.html');
        }
    }

    // 修改内容
    public function edit() {
        list($tpl) = $this->_Post(intval(\Phpcmf\Service::L('input')->get('id')));
        \Phpcmf\Service::V()->display($tpl);
    }

    // 删除内容
    public function del() {
        $this->_Del(
            \Phpcmf\Service::L('Input')->get_post_ids(),
            function($rows) {
                // 删除前的验证
                return dr_return_data(1, 'ok', $rows);
            },
            function($rows) {
                // 删除后的处理
                return dr_return_data(1, 'ok');
            },
            \Phpcmf\Service::M()->dbprefix($this->init['table'])
        );
    }

    /**
     * 获取内容
     * $id      内容id,新增为0
     * */
    protected function _Data($id = 0) {
        $row = parent::_Data($id);
        // 这里可以对内容进行格式化显示操处理
        return $row;
    }

    // 格式化保存数据
    protected function _Format_Data($id, $data, $old) {
		$data[1]['uid'] = $data[0]['uid'] = $this->uid;
		$datas = \Phpcmf\Service::L('Input')->post('data');
		
		
		$catid = $datas['catid'];
		$data[1]['catid'] = $catid;
        if (!$id) {
            // 当提交新数据时，把当前时间插入进去
            //$data[1]['inputtime'] = SYS_TIME;
        }
        return $data;
    }


    // 保存内容
    protected function _Save($id = 0, $data = [], $old = [], $func = null, $func2 = null) {
        return parent::_Save($id, $data, $old, function($id, $data, $old){
            // 验证数据
            /*
            if (!$data[1]['title']) {
                return dr_return_data(0, '标题不能为空！', ['field' => 'title']);
            }*/
            // 保存之前执行的函数，并返回新的数据
            if (!$id) {
                // 当提交新数据时，把当前时间插入进去
                //$data[1]['inputtime'] = SYS_TIME;
            }

            return dr_return_data(1, null, $data);
        }, function ($id, $data, $old) {
            // 保存之后执行的动作
        });
    }

}
